import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:casdon/common/widget/gradient_button.dart';
import 'package:casdon/common/widget/primary_button.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 首次打开app时，登录页面展示 隐私与用户协议弹窗（全屏遮罩）。
/// 返回值：点击“同意”返回 true，点击“不同意”或关闭返回 false/null。
Future<bool?> showFirstLaunchAggrementDialog({
  required BuildContext context,
  VoidCallback? onUserAgreementTap,
  VoidCallback? onPrivacyPolicyTap,
}) {
  return showGeneralDialog<bool>(
    context: context,
    barrierDismissible: false,
    barrierColor: Colors.black54,
    barrierLabel: 'agreement_dialog',
    pageBuilder: (_, __, ___) => const SizedBox.shrink(),
    transitionBuilder: (ctx, anim, __, ___) {
      final curved = Curves.easeOutBack.transform(anim.value);
      return Opacity(
        opacity: anim.value,
        child: Transform.scale(
          scale: 0.9 + 0.1 * curved,
          child: Center(
            child: Padding(
              padding: EdgeInsets.symmetric(horizontal: 18.w),
              child: Material(
                color: Colors.transparent,
                child: ConstrainedBox(
                  constraints: BoxConstraints(maxWidth: 360.w),
                  child: _AgreementDialogContent(
                    onUserAgreementTap: onUserAgreementTap,
                    onPrivacyPolicyTap: onPrivacyPolicyTap,
                  ),
                ),
              ),
            ),
          ),
        ),
      );
    },
  );
}

class _AgreementDialogContent extends StatefulWidget {
  const _AgreementDialogContent({
    this.onUserAgreementTap,
    this.onPrivacyPolicyTap,
  });

  final VoidCallback? onUserAgreementTap;
  final VoidCallback? onPrivacyPolicyTap;

  @override
  State<_AgreementDialogContent> createState() =>
      _AgreementDialogContentState();
}

class _AgreementDialogContentState extends State<_AgreementDialogContent> {
  bool _agreed = false;
  late final TapGestureRecognizer _userAgreementRecognizer;
  late final TapGestureRecognizer _privacyPolicyRecognizer;

  @override
  void initState() {
    super.initState();
    _userAgreementRecognizer = TapGestureRecognizer()
      ..onTap = widget.onUserAgreementTap;
    _privacyPolicyRecognizer = TapGestureRecognizer()
      ..onTap = widget.onPrivacyPolicyTap;
  }

  @override
  void dispose() {
    _userAgreementRecognizer.dispose();
    _privacyPolicyRecognizer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final grayText = Colors.black.withOpacity(0.8);

    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.08),
            blurRadius: 20,
            offset: const Offset(0, 10),
          ),
        ],
      ),
      padding: EdgeInsets.only(
        left: 16.w,
        right: 16.w,
        top: 21.h,
        bottom: 16.w,
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          Text(
            '提示条款',
            textAlign: TextAlign.center,
            style: context.textTheme.titleLarge!.copyWith(
              color: context.customColorThemeNonNull.textColor1,
            ),
          ),
          SizedBox(height: 21.h),
          Text(
            '欢迎使用凯度生活家App！',
            style: context.textTheme.bodyLarge!.copyWith(
              color: context.customColorThemeNonNull.textColor1,
            ),
          ),
          SizedBox(height: 16.h),
          RichText(
            text: TextSpan(
              style: context.textTheme.bodyMedium!.copyWith(
                color: context.customColorThemeNonNull.textColor1,
                height: 1.55,
              ),
              children: [
                const TextSpan(text: '请您务必审慎阅读并充分理解《'),
                TextSpan(
                  text: '用户协议',
                  style: context.textTheme.bodyMedium!.copyWith(
                    color: context.colorScheme.primary,
                  ),
                  recognizer: _userAgreementRecognizer,
                ),
                const TextSpan(text: '》与《'),
                TextSpan(
                  text: '隐私政策',
                  style: context.textTheme.bodyMedium!.copyWith(
                    color: context.colorScheme.primary,
                  ),
                  recognizer: _privacyPolicyRecognizer,
                ),
                const TextSpan(
                  text: '》各条款，包括但不限于用户注意事项、用户行为规范以及为向您提供服务而收集、使用、存储你个人信息的情形等。',
                ),
              ],
            ),
          ),
          SizedBox(height: 16.h),
          Text(
            '如您同意并接受本提示及相关协议内容，请点击“同意”，开始接受我们的服务。',
            style: context.textTheme.bodyMedium!.copyWith(
              color: context.customColorThemeNonNull.textColor1,
              height: 1.55,
            ),
          ),

          SizedBox(height: 26.h),
          Row(
            children: [
              Expanded(
                child: PrimaryButton(
                  text: '不同意',
                  borderRadius: 6,
                  width: 120.w,
                  height: 40.h,
                  textStyle: context.textTheme.headlineLarge!.copyWith(
                    color: context.colorScheme.primary,
                  ),
                  textColor: context.colorScheme.primary,
                  backgroundColor: const Color(0xFFF2F3FF),
                  onPressed: () => Navigator.of(context).pop(false),
                ),
              ),

              SizedBox(width: 12.w),
              Expanded(
                child: GradientButton(
                  text: '同意',
                  height: 40.h,
                  textStyle: context.textTheme.labelLarge?.copyWith(
                    color: Colors.white,
                  ),
                  enabled: true,
                  borderRadius: 6,
                  onPressed: () {
                    setState(() {
                      _agreed = true;
                    });
                    Navigator.of(context).pop(true);
                  },
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
